package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class blogMapper {
    //往数据库中插入一条新的博客blog信息
   public static void insert(Blog blog) throws SQLException {
       Connection connection=null;
       PreparedStatement statement=null;
       DatabaseOperator databaseOperator=new DatabaseOperator();
       connection=databaseOperator.getConnection();

       String sql = "insert into blog values(null, ?, ?, ?, now())";
       connection.prepareStatement(sql);
       statement = connection.prepareStatement(sql);
       statement.setString(1, blog.getTitle());
       statement.setString(2, blog.getContent());
       statement.setInt(3, blog.getUserId());

       int row=statement.executeUpdate();
       if(row!=0){
           System.out.println("新博客insert成功");
       }else{
           System.out.println("新博客insert失败");
       }
       databaseOperator.close(connection,statement,null);
   }

    //查询获取全部的文章
    public static List<Blog> selectAll() throws SQLException {
        Connection connection=null;
        PreparedStatement statement=null;
        DatabaseOperator databaseOperator=new DatabaseOperator();
        connection=databaseOperator.getConnection();
        List<Blog> blogList=new ArrayList<>();

        String sql="select*from blog order by postTime desc";
        statement=connection.prepareStatement(sql);
        ResultSet resultSet= statement.executeQuery();
        while(resultSet.next()){
            Blog blog=new Blog();
            blog.setBlogId(resultSet.getInt("blogId"));
            blog.setTitle(resultSet.getString("title"));
            String content=resultSet.getString("content");
            if(content.length()>50){
                blog.setContent(content.substring(0,50)+"...");
            }
            blog.setContent(content);
            blog.setUserId(resultSet.getInt("userId"));
            blog.setPostTime(resultSet.getTimestamp("postTime"));
            blogList.add(blog);
            System.out.println("yes");
        }
        databaseOperator.close(connection,statement,resultSet);
        return blogList;
    }

    //通过blogId从数据库中查询一条指定blog信息
    public static Blog selectOne(int blogId) throws SQLException {
        Connection connection=null;
        PreparedStatement statement=null;
        DatabaseOperator databaseOperator=new DatabaseOperator();
        connection=databaseOperator.getConnection();

        String sql="select*from blog where blogId=?";
        statement=connection.prepareStatement(sql);
        statement.setInt(1,blogId);

        ResultSet resultSet=statement.executeQuery();
        if(resultSet!=null&& resultSet.next()){
            Blog blog=new Blog();
            blog.setBlogId(resultSet.getInt("blogId"));
            blog.setUserId(resultSet.getInt("userId"));
            blog.setTitle(resultSet.getString("title"));
            blog.setContent(resultSet.getString("content"));
            blog.setPostTime(resultSet.getTimestamp("postTime"));
            return blog;
        }
        databaseOperator.close(connection,statement,resultSet);
        return null;
    }

    //通过博客id将数据库中删除一条博客信息(删除一条博客记录)
    public void delete(int blogId) throws SQLException {
        Connection connection = null;
        DatabaseOperator databaseOperator=new DatabaseOperator();
        PreparedStatement statement = null;
        try {
            connection = databaseOperator.getConnection();
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            databaseOperator.close(connection, statement, null);
        }
    }

}
